"""
Case Type   : GS_GLOBAL_CONFIG
Case Name   : 修改系统表gs_global_config已存在字段参数值
Create At   : 2022/07/22
Owner       : @zou_jialiang050
Description :
    1.向系统表gs_global_config添加一个字段
    2.查询系统表gs_global_config
    3.修改系统表gs_global_config已存在字段参数值
    4.再次查询系统表gs_global_config
    5.清理环境
Expect      :
    1.成功
    2.返回含新增参数
    3.成功
    4.返回含修改后的参数值
    5.成功
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Confirguration(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')

    def test_alter_global_configuration(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:向系统表gs_global_config添加一个字段 expect:成功'
        self.logger.info(step)
        alter_configuration = self.primary_sh.execut_db_sql(
            f'''alter global configuration with (test=text);''')
        self.logger.info(alter_configuration)
        self.assertIn('ALTER GLOBAL CONFIGURATION',
                      alter_configuration, '执行失败' + step)

        step = 'step2:查询系统表gs_global_config expect:返回含新增参数'
        self.logger.info(step)
        select_configuration = self.primary_sh.execut_db_sql(
            f'''select * from gs_global_config;''')
        self.logger.info(select_configuration)
        self.assertIn('test        | text', select_configuration, '执行失败' +
                      step)

        step = 'step3:修改系统表gs_global_config已存在字段参数值 expect:成功'
        self.logger.info(step)
        alter_configuration = self.primary_sh.execut_db_sql(
            f'''alter global configuration with (test=text1234);''')
        self.logger.info(alter_configuration)
        self.assertIn('ALTER GLOBAL CONFIGURATION',
                      alter_configuration, '执行失败' + step)

        step = 'step4:再次查询系统表gs_global_config ' \
               'expect:返回含修改后的参数值'
        self.logger.info(step)
        select_configuration = self.primary_sh.execut_db_sql(
            f'''select * from gs_global_config;''')
        self.logger.info(select_configuration)
        self.assertIn('test        | text1234', select_configuration,
                      '执行失败' + step)
        self.assertIn('(2 rows)', select_configuration, '执行失败' + step)

    def tearDown(self):
        step = 'step5:清理环境 expect:成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop global configuration test;''')
        self.logger.info(clean_environment)
        self.assertIn('Drop GLOBAL CONFIGURATION', clean_environment,
                      '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
